package edu.demo.recommend.service.impl;

import edu.demo.recommend.common.ResultCodeEnum;
import edu.demo.recommend.exception.RecommendBusinessException;
import edu.demo.recommend.pojo.Category;
import edu.demo.recommend.pojo.Seller;
import edu.demo.recommend.pojo.Shop;
import edu.demo.recommend.service.CategoryService;
import edu.demo.recommend.service.SellerService;
import edu.demo.recommend.mapper.ShopMapper;
import edu.demo.recommend.service.ShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author hb
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private CategoryService categoryService;

    @Resource
    private SellerService sellerService;
    @Resource
    private ShopMapper shopMapper;

    @Override
    public Shop create(Shop shop) {
        //1. 对商户进行校验
        Seller seller = sellerService.findById(shop.getSellerId());
        if(Objects.isNull(seller)){
            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR,"商户不存在");
        }
        //2. 判断商户是否被禁用
        if(seller.getDisabledFlag().intValue()==1){
            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR,"商户被下线");
        }
        
        //3. 对类别进行校验
        Category category = categoryService.getById(shop.getCategoryId());
        if(Objects.isNull(category)){
            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR,"类别不能为空");
        }
        shopMapper.insertSelective(shop);
        return shop;
    }

    @Override
    public Shop getById(Integer id) {
        Shop shop = shopMapper.selectByPrimaryKey(id);
        if(Objects.isNull(shop)){
            return null;
        }
        //查询关系属性
        Category category = categoryService.getById(shop.getCategoryId());
        Seller seller = sellerService.findById(shop.getSellerId());
        shop.setCategory(category);
        shop.setSeller(seller);
        return shop;
    }

    @Override
    public List<Shop> selectAll() {
        List<Shop> shops = shopMapper.selectAll();
        shops.forEach(shop->{
            shop.setCategory(categoryService.getById(shop.getCategoryId()));
            shop.setSeller(sellerService.findById(shop.getSellerId()));
        });
        return shops;
    }

    @Override
    public List<Shop> recommend(BigDecimal longitude, BigDecimal latitude) {
        List<Shop> shops = shopMapper.recommend(longitude, latitude);
        shops.forEach(shop->{
            shop.setCategory(categoryService.getById(shop.getCategoryId()));
            shop.setSeller(sellerService.findById(shop.getSellerId()));
        });
        return shops;
    }

    @Override
    public List<Shop> search(BigDecimal longitude, BigDecimal latitude, String keyword,Integer orderby, Integer categoryId,String tags) {
        List<Shop> shops = shopMapper.search(longitude, latitude,keyword,orderby,categoryId,tags);
        shops.forEach(shop->{
            shop.setCategory(categoryService.getById(shop.getCategoryId()));
            shop.setSeller(sellerService.findById(shop.getSellerId()));
        });
        return shops;
    }

    @Override
    public List<Map<String, Object>> searchGroupbyTags(String keyword, Integer categoryId, String tags) {
        return shopMapper.searchGroupbyTags(keyword,categoryId,tags);
    }
}
